package com.by.security;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.by.domain.pojo.SysUser;
import com.by.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @auth wzy
 * @date 2024/1/2 11:00
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);

        SysUser sysUser = sysUserService.getOne(queryWrapper);

        if (null == sysUser){
            throw new UsernameNotFoundException("用户不存在");
        }

        LoginUser loginUser = new LoginUser(sysUser,null);

        return loginUser;
    }
}
